/*
 * @FileName: [ReportController.java]
 * 
 * @Package com.bcinfo.iccard.admin.iccard.controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev$
 * $LastChangedDate$
 * $LastChangedBy$
 * 
 * @category ICCARD
 * 
 * @version 1.1
 * 
 * @author $Author$
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bcinfo.iccard.admin.iccard.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bcinfo.iccard.admin.controller.AbstractController;
import com.bcinfo.iccard.admin.iccard.entity.LabelValue;
import com.bcinfo.iccard.admin.iccard.entity.TreeNode;
import com.bcinfo.iccard.model.ComboCard;
import com.bcinfo.iccard.model.Merchant;
import com.bcinfo.iccard.service.ComboCardService;
import com.bcinfo.iccard.service.FinancialReportService;
import com.bcinfo.iccard.service.MerchantService;

/**
 * 报表controler <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 1、消费结算日报表<br/>
 * 2、消费结算月报表<br/>
 * 3、消费结算季报表<br/>
 * 4、消费结算年报表<br/>
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 * 代码使用示例
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/iccard/report")
public class ReportController extends AbstractController
{
    // TODO 系统开始日期
    private static final String    START_DATE = "20131126";

    @Autowired
    private FinancialReportService _financialReportService;
    @Autowired
    private MerchantService        _merchantService;

    @Autowired
    private ComboCardService       _comboCardService;

    @ResponseBody
    @RequestMapping("/getDateTree")
    public List<TreeNode> listDate()
    {
        List<TreeNode> list = new ArrayList<TreeNode>();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        try
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(yyyyMMdd.parse(START_DATE));
            TreeNode year = new TreeNode();
            year.setId(String.valueOf(calendar.get(Calendar.YEAR)));
            year.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
            year.setState("open");
            year.setChildren(new ArrayList<TreeNode>());
            TreeNode month = new TreeNode();
            month.setId(yyyyMM.format(calendar.getTime()));
            month.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1) + "月");
            month.setState("closed");
            month.setChildren(new ArrayList<TreeNode>());
            year.getChildren().add(month);
            list.add(year);
            while (calendar.getTimeInMillis() < (System.currentTimeMillis() - 48 * 60 * 60 * 1000))
            {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                // 跨年
                if (isNewDayOfYear(calendar))
                {
                    year = new TreeNode();
                    year.setId(String.valueOf(calendar.get(Calendar.YEAR)));
                    year.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
                    year.setState("open");
                    year.setChildren(new ArrayList<TreeNode>());
                    list.add(year);
                }
                // 跨月
                if (isNewDayOfMonth(calendar))
                {
                    month = new TreeNode();
                    month.setId(yyyyMM.format(calendar.getTime()));
                    month.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1) + "月");
                    month.setState("closed");
                    month.setChildren(new ArrayList<TreeNode>());
                    year.getChildren().add(month);
                }
                //
                TreeNode day = new TreeNode();
                day.setId(yyyyMMdd.format(calendar.getTimeInMillis()));
                day.setText(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + "日");
                month.getChildren().add(day);
            }
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    @ResponseBody
    @RequestMapping("/getMonthTree")
    public List<TreeNode> listMonth()
    {
        List<TreeNode> list = new ArrayList<TreeNode>();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");

        try
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(yyyyMMdd.parse(START_DATE));
            TreeNode year = new TreeNode();
            year.setId(String.valueOf(calendar.get(Calendar.YEAR)));
            year.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
            year.setState("open");
            year.setChildren(new ArrayList<TreeNode>());
            TreeNode month = new TreeNode();
            month.setId(yyyyMM.format(calendar.getTime()));
            month.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1) + "月");
            month.setChildren(new ArrayList<TreeNode>());
            year.getChildren().add(month);
            list.add(year);
            while (!yyyyMM.format(calendar.getTimeInMillis()).equals(yyyyMM.format(System.currentTimeMillis())))
            {
                calendar.add(Calendar.MONTH, 1);
                // 跨年
                if (isNewMonthOfYear(calendar))
                {
                    year = new TreeNode();
                    year.setId(String.valueOf(calendar.get(Calendar.YEAR)));
                    year.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
                    year.setState("open");
                    year.setChildren(new ArrayList<TreeNode>());
                    list.add(year);
                }
                month = new TreeNode();
                month.setId(yyyyMM.format(calendar.getTime()));
                month.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1) + "月");
                month.setChildren(new ArrayList<TreeNode>());
                year.getChildren().add(month);
            }
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    @ResponseBody
    @RequestMapping("/getQuarterTree")
    public List<TreeNode> listQuarter()
    {
        List<TreeNode> list = new ArrayList<TreeNode>();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");

        try
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(yyyyMMdd.parse(START_DATE));
            TreeNode year = null;
            while (calendar.getTimeInMillis() < System.currentTimeMillis())
            {
                int season = (calendar.get(Calendar.MONTH)) / 3 + 1;
                // 跨年
                if (season == 1 || year == null)
                {
                    year = new TreeNode();
                    year.setId(String.valueOf(calendar.get(Calendar.YEAR)));
                    year.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
                    year.setState("open");
                    year.setChildren(new ArrayList<TreeNode>());
                    list.add(year);
                }
                TreeNode quarter = new TreeNode();
                quarter.setId(calendar.get(Calendar.YEAR) + "." + season);
                quarter.setText("第" + String.valueOf(season) + "季度");
                quarter.setChildren(new ArrayList<TreeNode>());
                year.getChildren().add(quarter);
                //
                calendar.add(Calendar.MONTH, 3);
            }
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    @ResponseBody
    @RequestMapping("/getYearTree")
    public List<TreeNode> listYear()
    {
        List<TreeNode> list = new ArrayList<TreeNode>();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");

        try
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(yyyyMMdd.parse(START_DATE));
            while (calendar.getTimeInMillis() < System.currentTimeMillis())
            {

                TreeNode year = new TreeNode();
                year.setId(String.valueOf(calendar.get(Calendar.YEAR)));
                year.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
                list.add(year);
                calendar.add(Calendar.YEAR, 1);
            }
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    // 跨年检查
    private boolean isNewMonthOfYear(Calendar calendar)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        long time = calendar.getTimeInMillis();
        if (sdf.format(time).endsWith("01"))
            return true;
        return false;
    }

    // 跨月检查
    private boolean isNewDayOfMonth(Calendar calendar)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        long time = calendar.getTimeInMillis();
        if (sdf.format(time).endsWith("01"))
            return true;
        return false;
    }

    // 跨年检查
    private boolean isNewDayOfYear(Calendar calendar)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        long time = calendar.getTimeInMillis();
        if (sdf.format(time).endsWith("0101"))
            return true;
        return false;
    }

    // 取商家列表，autocomplete选择器
    @ResponseBody
    @RequestMapping("/getSellers")
    public List<LabelValue> listSellers(String keyword, int maxRows)
    {
        List<LabelValue> list = new ArrayList<LabelValue>();
        if (StringUtils.isBlank(keyword))
            return list;
        try
        {
            List<Merchant> sellers = _merchantService.findByNameCode(keyword, maxRows);
            for (Merchant m : sellers)
            {
                list.add(new LabelValue(m.getMerchantName(), m.getMerchantCode()));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //
        return list;
    }

    // 取所有商家列表/弹出层选择器
    @ResponseBody
    @RequestMapping("/getAllSellers")
    public List<LabelValue> listAllSellers()
    {
        List<LabelValue> list = new ArrayList<LabelValue>();
        try
        {
            List<Merchant> sellers = _merchantService.findAll();
            for (Merchant m : sellers)
            {
                list.add(new LabelValue(m.getMerchantName(), m.getMerchantCode()));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //
        return list;
    }

    // 取套餐列表，autocomplete选择器
    @ResponseBody
    @RequestMapping("/getPackages")
    public List<LabelValue> listPackages(String keyword, int maxRows)
    {
        List<LabelValue> list = new ArrayList<LabelValue>();
        if (StringUtils.isBlank(keyword))
            return list;
        try
        {
            List<ComboCard> sellers = _comboCardService.findByNameCode(keyword, maxRows);
            for (ComboCard m : sellers)
            {
                list.add(new LabelValue(m.getComboCardName(), String.valueOf(m.getComboCardId())));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //
        return list;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.admin.controller.AdminController#getMouduleCode()
     */

    @Override
    public String getMouduleCode()
    {
        return null;
    }

}
